// 获取DOM元素
const diceContainer = document.getElementById('diceContainer');
const rollButton = document.getElementById('rollButton');
const resultDisplay = document.getElementById('result');

// 初始化骰子面数
let diceValues = [1, 2, 3, 4, 5, 6]; // 初始值

// 博饼规则和等级
const BOBING_RULES = {
    '状元插金花': { condition: (counts) => counts[3] === 4 && counts[0] === 2, rank: 1 },
    '状元': { 
        condition: (counts) => counts[3] >= 4 || counts.some(count => count >= 5), 
        rank: 2 
    },
    '对堂': { 
        condition: (counts) => counts.every(count => count === 1), 
        rank: 3 
    },
    '四进': { 
        condition: (counts) => counts.some((count, index) => index !== 3 && count === 4), 
        rank: 4 
    },
    '三红': { 
        condition: (counts) => counts[3] === 3, 
        rank: 5 
    },
    '二举': { 
        condition: (counts) => counts[3] === 2, 
        rank: 6 
    },
    '一秀': { 
        condition: (counts) => counts[3] === 1, 
        rank: 7 
    }
};

// 生成随机骰子点数
function rollDice() {
    return Array.from({length: 6}, () => Math.floor(Math.random() * 6) + 1);
}

// 更新骰子显示
function updateDiceDisplay(values) {
    const diceElements = diceContainer.querySelectorAll('.dice');
    diceElements.forEach((dice, index) => {
        // 更新骰子显示的点数
        // 如果你使用文字点数，如⚀到⚅
        const diceFaces = ['⚀', '⚁', '⚂', '⚃', '⚄', '⚅'];
        dice.textContent = diceFaces[values[index] - 1];
        
        // 如果你使用图片，可以这样更新：
        // dice.style.backgroundImage = `url('assets/dice${values[index]}.png')`;
    });
}

// 分析骰子结果，判断奖项
function analyzeResult(values) {
    // 统计每个点数出现的次数
    const counts = [0, 0, 0, 0, 0, 0]; // 分别对应1-6点
    values.forEach(value => {
        counts[value - 1]++;
    });
    
    // 检查每个规则
    for (const [award, rule] of Object.entries(BOBING_RULES)) {
        if (rule.condition(counts)) {
            return award;
        }
    }
    
    return '无奖励';
}

// 掷骰子动画和逻辑
async function performRoll() {
    // 禁用按钮，防止连续点击
    rollButton.disabled = true;
    resultDisplay.textContent = '掷骰中...';
    
    // 添加滚动动画
    const diceElements = diceContainer.querySelectorAll('.dice');
    diceElements.forEach(dice => {
        dice.classList.add('rolling');
    });
    
    // 模拟骰子滚动效果
    let rollCount = 0;
    const maxRolls = 10; // 滚动次数
    const rollInterval = setInterval(() => {
        const tempValues = rollDice();
        updateDiceDisplay(tempValues);
        rollCount++;
        
        if (rollCount >= maxRolls) {
            clearInterval(rollInterval);
            
            // 生成最终结果
            diceValues = rollDice();
            updateDiceDisplay(diceValues);
            
            // 移除动画
            diceElements.forEach(dice => {
                dice.classList.remove('rolling');
            });
            
            // 分析并显示结果
            const result = analyzeResult(diceValues);
            resultDisplay.textContent = `结果：${result}！`;
            resultDisplay.style.color = result === '无奖励' ? '#95a5a6' : '#e74c3c';
            
            // 重新启用按钮
            rollButton.disabled = false;
        }
    }, 100);
}

// 添加事件监听器
rollButton.addEventListener('click', performRoll);

// 初始化显示
updateDiceDisplay(diceValues);